/*=======================================*/
/*	用户注册功能的js                           */
/*	@author dbBin                       */
/*=======================================*/

$(document).ready(function(){
	$("#userName").focus(function(){
		$("#userName").css("borderColor","#1D7AD9");
		$("#user_ts1").css("color","grey");
		$("#user_ts1").css("display","block");
		$("#user_ts2").css("display","none");
	});
	$("#userName").blur(function(){
		if($("#userName").val()=="" || $("#userName").val() == null)
		{
			$("#user_ts1").css("display","none");
			$("#user_ts2").css("display","block");
			$("#userName").css("borderColor","red");
		}
		else{
			$("#userName").css("borderColor","#999");
		}
		
	});
	$("#passWord").focus(function(){
		$("#passWord").css("borderColor","#1D7AD9");
		$("#pass1_ts1").css("display","block");
		$("#pass1_ts1").css("color","grey");
		$("#pass1_ts2").css("display","none");
	});
	$("#passWord").blur(function(){
		if($("#passWord").val()=="" || $("#passWord").val() == null)
		{
			$("#pass1_ts1").css("display","none");
			$("#pass1_ts2").css("display","block");
			$("#passWord").css("borderColor","red");
		}else{
			$("#passWord").css("borderColor","#999");
		}
	});
	$("#rePassword").focus(function(){
		$("#rePassword").css("borderColor","#1D7AD9");
		$("#pass2_ts1").css("display","block");
		$("#pass2_ts1").css("color","grey");
		$("#pass2_ts2").css("display","none");
	});
	
	$("#rePassword").blur(function(){
		if($("#rePassword").val()=="" || $("#rePassword").val() == null)
		{
			$("#pass2_ts1").css("display","none");
			$("#pass2_ts2").css("display","block");
			$("#rePassword").css("borderColor","red");
		}else{
			$("#rePassword").css("borderColor","#999");
		}
		
	});
	$("#userRegistBtn").on('click',function(){
		nameReg=/^[a-zA-Z0-9_]{3,50}$/;
		passReg=/^[A-Za-z0-9_]{6,16}$|^(?!\d{6,8}$)/;
		if(!nameReg.test($("#userName").val())){
			$("#user_ts1").css("color","red");
			$("#user_ts1").css("display","block");
			$("#user_ts2").css("display","none");
			$("#userName").css("borderColor","red");
		}
		else if(!passReg.test($("#passWord").val())){
			$("#pass1_ts1").css("color","red");
			$("#pass1_ts1").css("display","block");
			$("#pass1_ts2").css("display","none");
			$("#passWord").css("borderColor","red");
		}
		else if($("#rePassword").val() != $("#passWord").val()){
			$("#pass2_ts1").html("*两次输入密码不一致");
			$("#pass2_ts1").css("color","red");
			$("#pass2_ts1").css("display","block");
			$("#pass2_ts2").css("display","none");
		}
		else{
			registers();
		}
		
    });
	randomImg();   //加载页面时生成验证码
});

function randomImg() {
	var random = new Date().getTime();
	$("#randomString").val(random);
	$("#authCodeImg").attr("src", "/valid/image/getValidImage?type=syslogin&randomString="+random);
}

function registers(){
	var btn = $("#userRegistBtn");
    btn.button('loading');
	var datasent = $("#userRegistForm").serializeObject();
	params = JSON.stringify(datasent); 
	$.ajax({
		type : "POST",
		url : "/customjsp/register/registerInfo",
		dataType : "json",
		contentType : "application/json;charset=utf-8",
		data : params,
		async : false,  //同步或异步
		success : function(data) {
			if(data.resultCode == 0){   //注册成功
				window.location.href="/customjsp/login";
			}else{
				randomImg();   //加载页面时生成验证码
				alert(data.resultMessage);
			}
			btn.button('reset');
		}
	});
}

